﻿using Microsoft.Extensions.Logging;
using NetCore.Common.Redis;
using NetCore.IRepository;
using NetCore.IRepository.Order_LayerwinDingmachineEquipment;
using NetCore.IRepository.Productionproductinfo;
using NetCore.IRepository.SysOndutytype;
using NetCore.IRepository.User;
using NetCore.Model.Enity;
using NetCore.Model.VeiwModels;
using NetCore.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCore.Services.Statistics
{
    public class EmployeeSalaryCalculationServices : BaseService<OrderLayerwindingmachineequipment>, NetCore.IServices.Statistics.IEmployeeSalaryCalculationServices
    {
        private readonly IMain_RedisCacheManager _redisCacheManager;
        private readonly ISlave_RedisCacheManager _slave_RedisCacheManager;
        private readonly ILogger<EmployeeSalaryCalculationServices> _logger;
        private readonly IUserRepository _userRepository;
        private readonly IOrder_layerwindingmachineequipmentRepository _order_LayerwindingmachineequipmentRepository;
        private readonly IProductionproductinfo _productionproductinfo;
        private readonly ISysOndutytype _sysOndutytype;

        public EmployeeSalaryCalculationServices(IBaseRepository<OrderLayerwindingmachineequipment> baseRepository, ILogger<EmployeeSalaryCalculationServices> logger, IUserRepository userRepository, IOrder_layerwindingmachineequipmentRepository order_LayerwindingmachineequipmentRepository,
            IMain_RedisCacheManager redisCacheManager,
            IProductionproductinfo productionproductinfo,
            ISysOndutytype sysOndutytype,
            ISlave_RedisCacheManager slave_RedisCacheManager) : base(baseRepository)
        {
            _logger = logger;
            _userRepository = userRepository;
            _order_LayerwindingmachineequipmentRepository = order_LayerwindingmachineequipmentRepository;
            _redisCacheManager = redisCacheManager;
            _productionproductinfo = productionproductinfo;
            _sysOndutytype = sysOndutytype;
            _slave_RedisCacheManager = slave_RedisCacheManager;
        }

        /// <summary>
        /// 统计所有数据员工工资
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<bool> StatisticsEmployeeWages()
        {
            _ = new List<Productionproductinfo>();
            List<StatisticsEmployeeWagesResult> statisticsEmployeeWagesResults = new List<StatisticsEmployeeWagesResult>();
            try
            {
                //获取所有产品价格
                List<Productionproductinfo> productionproductinfos = await _productionproductinfo.GetProductionproductinfos();
                //获取用户id
                foreach (int userid in await _userRepository.GetUserid())
                {
                    //工资
                    decimal totalsalary = 0;
                    //获取当前用户生产的订单信息
                    foreach (Productionproductinfo productionproductinfo in productionproductinfos)
                    {
                        List<SysOndutytype> sysOndutytypes = await _sysOndutytype.GetSysOndutytypes();
                        foreach (SysOndutytype sysOndutytype in sysOndutytypes)
                        {
                            switch (sysOndutytype.Name)
                            {
                                case "早班":
                                    long morningShiftCount = await _order_LayerwindingmachineequipmentRepository.GetOredrInfo(userid, productionproductinfo.Id, Convert.ToDateTime(sysOndutytype.Starttime), Convert.ToDateTime(sysOndutytype.Endtime));

                                    totalsalary += morningShiftCount * productionproductinfo.MorningshiftEmployeesalary;
                                    break;

                                case "中班":
                                    long middleShiftCount = await _order_LayerwindingmachineequipmentRepository.GetOredrInfo(userid, productionproductinfo.Id, Convert.ToDateTime(sysOndutytype.Starttime), Convert.ToDateTime(sysOndutytype.Endtime));

                                    totalsalary += middleShiftCount * productionproductinfo.MiddleshiftEmployeesalary;
                                    break;

                                case "晚班":
                                    long eveningShiftCount = await _order_LayerwindingmachineequipmentRepository.GetOredrInfo(userid, productionproductinfo.Id, Convert.ToDateTime(sysOndutytype.Starttime), Convert.ToDateTime(sysOndutytype.Endtime));

                                    totalsalary += eveningShiftCount * productionproductinfo.EveningshiftEmployeesalary;
                                    break;
                            }
                        }
                    }

                    statisticsEmployeeWagesResults.Add(new StatisticsEmployeeWagesResult() { Usernmae = "用户id：" + userid, Wages = totalsalary });
                }

                _redisCacheManager.Set("StatisticsEmployeeWages", JsonConvert.SerializeObject(statisticsEmployeeWagesResults, Formatting.Indented), (DateTime.Now - DateTime.Now.AddMinutes(30)).Duration());
                List<StatisticsEmployeeWagesResult> statisticsEmployeeWagesResults1 = _slave_RedisCacheManager.Get<List<StatisticsEmployeeWagesResult>>("StatisticsEmployeeWages");
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return false;
            }
        }
    }
}